package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.mapper.StatementItemMapper;
import cn.wolfcode.car.business.qo.StatementItemQueryObject;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class StatementItemServiceImpl implements IStatementItemService {

    @Autowired
    private StatementItemMapper statementItemMapper;
    @Autowired
    private IStatementService statementService;
    @Autowired
    private IAppointmentService appointmentService;


    @Override
    public TablePageInfo<StatementItem> query(StatementItemQueryObject qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<StatementItem>(statementItemMapper.selectForList(qo));
    }


    @Override
    public void save(StatementItem statementItem) {
        statementItemMapper.insert(statementItem);
    }

    @Override
    public StatementItem get(Long id) {
        return statementItemMapper.selectOne(id);
    }


    @Override
    public void update(StatementItem statementItem) {
        statementItemMapper.update(statementItem);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            statementItemMapper.delete(dictId);
        }
    }

    @Override
    public List<StatementItem> list() {
        return statementItemMapper.selectAll();
    }

    @Override
    public void saveItems(List<StatementItem> list) {
        StatementItem item = list.remove(list.size() - 1);
        Long statementId = item.getStatementId();
        BigDecimal discountAmount = item.getItemPrice();

        statementItemMapper.deleteBystatementId(statementId);

        BigDecimal totalAmount = new BigDecimal(0);
        BigDecimal totalQuantity = new BigDecimal(0);
        for (StatementItem statementItem : list) {
            BigDecimal itemQuantity = new BigDecimal(statementItem.getItemQuantity() + "");
            totalAmount = totalAmount.add(statementItem.getItemPrice().multiply(itemQuantity));
            totalQuantity =totalQuantity.add(itemQuantity);
            statementItemMapper.insert(statementItem);
        }
        Statement statement = new Statement();
        statement.setId(statementId);
        statement.setDiscountAmount(discountAmount);
        statement.setTotalAmount(totalAmount);
        statement.setTotalQuantity(totalQuantity);
        statementService.updateAmount(statement);
    }

    @Override
    public Long creatItem(Long appointmentId) {

        Statement statement = statementService.getByAppointmentId(appointmentId);
        Appointment appointment = appointmentService.get(appointmentId);
        appointmentService.changeStatus(appointmentId);

        if (statement == null) {
            statement = new Statement();
            statement.setCustomerName(appointment.getCustomerName());
            statement.setCustomerPhone(appointment.getCustomerPhone());
            statement.setActualArrivalTime(appointment.getActualArrivalTime());
            statement.setLicensePlate(appointment.getLicensePlate());
            statement.setCarSeries(appointment.getCarSeries());
            statement.setServiceType(appointment.getServiceType().longValue());
            statement.setInfo(appointment.getInfo());
            statement.setAppointmentId(appointmentId);
            statement.setCreateTime(new Date());
            statementService.save(statement);
        }

        return statementService.getByAppointmentId(appointmentId).getId();

    }

}
